package com.qishuihe.common.utils.threads;

import com.qishuihe.common.utils.ListUtils;
import com.qishuihe.running.runningrespository.tablecarddata.TableCardDataService;
import com.qishuihe.running.runningrespository.tablecarddata.entity.TableCardDataEntity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.stream.Collectors;


public class MegerDataCallable implements Callable<MegerDataVO<List<Map<String,String>>>> {


    private TableCardDataService tableCardDataService;

    private MegerDataVO<List<TableCardDataEntity>> megerDataVO;

    public MegerDataCallable(TableCardDataService tableCardDataService,MegerDataVO<List<TableCardDataEntity>> megerDataVO) {

        this.tableCardDataService = tableCardDataService;
        this.megerDataVO = megerDataVO;
    }
    public MegerDataVO<List<Map<String,String>>> call() throws Exception {

        MegerDataVO<List<Map<String,String>>>  tempMegerDataVO = new MegerDataVO<List<Map<String,String>>>();

        if(ListUtils.isEmpty(megerDataVO.getObj())){
            return tempMegerDataVO;
        }
        tempMegerDataVO.setOrder(megerDataVO.getOrder());
        List<TableCardDataEntity> ls =megerDataVO.getObj();
        Map<String,List<TableCardDataEntity>> map = ls.stream().collect(Collectors.groupingBy(TableCardDataEntity::getUuid));
        List<Map<String,String>> tempList= new ArrayList<Map<String,String>>();
        List<String> uuids = new ArrayList<String>();
        for(TableCardDataEntity tableCardDataEntity:ls) {
            if(uuids.contains(tableCardDataEntity.getUuid())) {
                tempList.get(uuids.indexOf(tableCardDataEntity.getUuid())).put(tableCardDataEntity.getCode(),tableCardDataEntity.getValue());
            }else {
                uuids.add(tableCardDataEntity.getUuid());
                Map<String,String> map_= new HashMap<String,String>();
                map_.put(tableCardDataEntity.getCode(),tableCardDataEntity.getValue());
                tempList.add(map_);
            }

        }
        tempMegerDataVO.setObj(tempList);
        System.out.println(megerDataVO.getOrder());
        return tempMegerDataVO;
    }

}
